<?php

namespace app\Api\controller;

use app\common\controller\Api;
use app\common\model\PayBill as payBillModel;

class PayBill extends Api
{
    
    public function create()
    {
        $user_info = model('user')->get($this->userId);
        if(!$user_info->isbuy) {
            $this->error['msg'] = '用户被限制购买, 请联系客服';
            return $this->error;
        }
        $amount = $this->request->post('amount');
        $room_bn = $this->request->post('room_bn');
        if(empty($amount) || empty($room_bn)) {
            $this->error['msg'] = '参数异常';
            return $this->error;
        }
        $pay_bill_model = new payBillModel();
        return $pay_bill_model->pay($room_bn, $amount, $this->userId,$this->request->post('order_ids', ''), $this->request->post('taocan_ids', ''));  
    }
    
    public function getList()
    {
        $page = $this->request->get('page', 1);
        $size = $this->request->get('size', 20);
        $begintime=strtotime(date("Y-m-d",strtotime("-3 day"))." 0:0:0");
        $pay_bill_model = new payBillModel();
        $this->result['data'] = $pay_bill_model->where('user_id', $this->userId)
            ->where('ctime', ['EGT',$begintime],['ELT',time()])->order('id', 'desc')->limit(($page-1)*$size, $size)->select();
        $this->result['data'] = obj_to_array($this->result['data']);
        foreach ($this->result['data'] as &$info) {
            $info['ctime'] = date('Y-m-d H:i:s', $info['ctime']);
        }
        return $this->result;
    }
    
    public function getBill()
    {
        $hour=date("G");
        if($hour < 3){
            $begintime=strtotime(date("Y-m-d",strtotime("-1 day"))." 0:0:0");
            $endtime=strtotime(date("Y-m-d",strtotime("-1 day"))." 23:59:59");
        }else{
            $begintime=strtotime(date("Y-m-d",time())." 0:0:0");
            $endtime=strtotime(date("Y-m-d",time())." 23:59:59");
        }
        $where = [];
        $where[] = ['booking_time', ['EGT',$begintime],['ELT',$endtime],'and'];
        if($this->request->post('room_bn')) {
            $where[] = [ 'bn','=', $this->request->post('room_bn')];
        }
        $where[] = ['pay_status', '=', 1];
        $booking = model('RoomBooking')->field('*')->where($where)->select();
        if($booking->isEmpty()){//房间未顶
            $data['flg'] = 1;
            $data['msg'] = "请先预定房间";
            $this->result['data'] = $data;
            return $this->result;
        }
        if($booking[0]['to_shop'] != 1){//客人未到店
            $data['flg'] = 2;
            $data['msg'] = "请先到店";
            $this->result['data'] = $data;
            return $this->result;
        }
        if($booking[0]['user_id'] != $this->userId){//房间不是本人预定
            $data['flg'] = 3;
            $this->result['data'] = $data;
            return $this->result;
        }


        $data['total_amount'] = 0;
        $data['maidan_amount'] = 0;
        if($hour < 3){
            $begintime = strtotime(date("Y-m-d",strtotime("-1 day"))." 3:0:0");
            $endtime = time();
        }else{
            $begintime = strtotime(date("Y-m-d")." 3:0:0");
            $endtime = time();
        }
        //订单
        $condition = [
            //['after_status', '=', '0'],
            //['user_id', '=', $this->userId],
            ['pay_status', '=', '2'],
            ['ctime', ['EGT',$begintime],['ELT',$endtime],'and'],
            ['status', 'neq', '3'],
        ];
        if($this->request->post('room_bn')) {
            $condition[] = ['room_bn', '=', $this->request->post('room_bn')];
        }
        $field = ['order_id', 'goods_amount', 'order_amount', 'status', 'payment_code', 'room_bn', 'after_status', 'ctime'];
        $order_list = model('Order')->field($field)->where($condition)->select();
        foreach ($order_list as &$info) {
            $data['total_amount'] = $data['total_amount'] + $info->order_amount;
            $info->date_time = date('Y-m-d H:i:s', $info->ctime);
            $data['order_ids'][] =  $info->order_id;
        }
        $data['order'] = $order_list;
        //套餐
        $taocan_condition = [
            //['after_status', '=', '0'],
            //['user_id', '=', $this->userId],
            ['pay_status', '=', '1'],
            ['ctime', ['EGT',$begintime],['ELT',$endtime],'and']
        ];
        if($this->request->post('room_bn')) {
            $taocan_condition[] = ['room_bn', '=', $this->request->post('room_bn')];
        }
        $taocan_list = model('TaocanOrder')->where($taocan_condition)->select();
        foreach ($taocan_list as &$info) {
            $data['total_amount'] = $data['total_amount'] + $info->price;
            $info->date_time = date('Y-m-d H:i:s', $info->ctime);
            $data['taocan_ids'][] =  $info->id;
        }
        $data['taocan'] = $taocan_list;

        //埋单信息
        $maidan_condition = [
            ['pay_status', '=', '1'],
            ['ctime', ['EGT',$begintime],['ELT',$endtime],'and']
        ];
        if($this->request->post('room_bn')) {
            $maidan_condition[] = ['room_bn', '=', $this->request->post('room_bn')];
        }
        $maidan_list = model('PayBill')->where($maidan_condition)->select();
        foreach ($maidan_list as &$info) {
            $data['maidan_amount'] = $data['maidan_amount'] + $info->amount;
            $info->date_time = date('Y-m-d H:i:s', $info->ctime);
            $data['maidan_ids'][] =  $info->id;
        }
        $data['maidan'] = $maidan_list;


        //后结单
        /*$condition1 = [
            //['after_status', '=', '0'],
            //['user_id', '=', $this->userId],
            ['pay_status', '=', '2'],
            ['payment_code', '=', 'after_pay'],
            ['ctime', ['EGT',$begintime],['ELT',$endtime],'and']

            //['status', 'neq', '3'],
        ];
        if($this->request->post('room_bn')) {
            $condition1[] = ['room_bn', '=', $this->request->post('room_bn')];
        }
        $field = ['order_id', 'goods_amount', 'order_amount', 'status', 'payment_code', 'room_bn', 'after_status', 'ctime'];
        $order_list = model('Order')->field($field)->where($condition1)->select();
        foreach ($order_list as &$info) {
            $data['houjie'] = $data['houjie'] + $info->order_amount;
            $info->date_time = date('Y-m-d H:i:s', $info->ctime);
            $data['order_ids'][] =  $info->order_id;
        }
        $data['order1'] = $order_list;
        $taocan_condition1 = [
            //['after_status', '=', '0'],
            //['user_id', '=', $this->userId],
            ['pay_status', '=', '1'],
            ['payment_code', '=', 'after_pay'],
            ['ctime', ['EGT',$begintime],['ELT',$endtime],'and']
        ];
        if($this->request->post('room_bn')) {
            $taocan_condition1[] = ['room_bn', '=', $this->request->post('room_bn')];
        }
        $taocan_list = model('TaocanOrder')->where($taocan_condition1)->select();
        foreach ($taocan_list as &$info) {
            $data['houjie'] = $data['houjie'] + $info->price;
            $info->date_time = date('Y-m-d H:i:s', $info->ctime);
            $data['taocan_ids'][] =  $info->id;
        }
        $data['taocan1'] = $taocan_list;
        */
        $data['flg'] = 0;
        $this->result['data'] = $data;
        return $this->result;
    }


}
